﻿package plugin.newguanwei
{
    import cmd.*;
    import com.core.*;
    import com.sh.game.display.*;
    import com.sh.game.global.*;
    import flash.events.*;
    import flash.geom.*;
    import manager.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import pluginfw.nio.*;
    import uiresource.guanzhi.*;
    import view.*;
    import view.light.*;
    import view.mornExtends.*;
    import vo.*;

    public class NewGuanWeiCtrl extends NewguanweiUI
    {
        private var close:Function;
        private var cfggw:Array;
        private var gzlv:int = 0;
        private var roleLv:int = 0;
        private var storeID:Array;
        private var cfgstore:Object;
        private var cfgitem:Object;
        private var cfglevel:Object;
        private var user:Object;
        private var roledata:Object;
        private var currpage:int = 0;
        private var shengwang:int;
        private var _fenluFilterShow:Boolean = false;
        public var yupeiCtrl:YupeiCtrl;
        private var guanweiMc:XMLMovieClip;
        public static const guanweiEffect:Class = NewGuanWeiCtrl_guanweiEffect;

        public function NewGuanWeiCtrl(param1:Function)
        {
            this.storeID = [209, 210];
            this.close = param1;
            return;
        }// end function

        override protected function createChildren() : void
        {
            viewClassMap["uiresource.guanzhi.YupeiUI"] = YupeiCtrl;
            loadUI("Newguanwei.xml");
            this.yupeiCtrl = yupei as YupeiCtrl;
            return;
        }// end function

        override protected function initializeView() : void
        {
            super.initializeView();
            var _loc_1:* = Control.create(NewGuanWeiCon, this);
            ModelProxy.addObserver("guanwei", _loc_1);
            this.yupeiCtrl.visible = false;
            rigth_box.visible = false;
            this.getData();
            this.addEvent();
            return;
        }// end function

        private function completeFunc1(param1:Object) : void
        {
            var _loc_2:* = new XML(new guanweiEffect());
            this.guanweiMc = new XMLMovieClip("guanwei", _loc_2);
            this.guanweiMc.mouseChildren = false;
            this.guanweiMc.mouseEnabled = false;
            this.guanweiMc.x = 15;
            this.guanweiMc.y = -150;
            this.guanweiMc.visible = false;
            this.guanweiMc.play();
            this.addChild(this.guanweiMc);
            return;
        }// end function

        public function shengjinguanwei() : void
        {
            this.init();
            if (this.guanweiMc)
            {
                this.guanweiMc.visible = true;
                this.guanweiMc.playFromTo(-1, (this.guanweiMc.totalFrame - 1), new Handler(this.doEnd1));
            }
            return;
        }// end function

        private function doEnd1() : void
        {
            this.guanweiMc.visible = false;
            this.guanweiMc.stop();
            return;
        }// end function

        private function getData() : void
        {
            this.cfggw = ConfigDictionary.data.guanwei;
            this.cfgstore = ConfigDictionary.data.storeNew;
            this.cfgitem = ConfigDictionary.data.item;
            this.cfglevel = ConfigDictionary.data.level;
            list.renderHandler = new Handler(this.renderHandler);
            list.array = this.storeID;
            return;
        }// end function

        public function init(param1:int = 0) : void
        {
            if (this.guanweiMc == null)
            {
                App.mloader3.loadSWF(Config.getUrl("assets/images/mainui/guanwei.swf"), 1, new Handler(this.completeFunc1));
            }
            this.user = UserData.getUserData();
            this.roledata = UserData.getOnlineData();
            if (this.roledata.guanzhilingqu == 1)
            {
                lingquLink.visible = false;
            }
            else
            {
                lingquLink.visible = true;
            }
            this.roleLv = this.user.level;
            if (!this.user.guanzhi)
            {
                this.user.guanzhi = 0;
            }
            if (this.roledata.guanzhilingqu == 1 || this.user.guanzhi < 1)
            {
                lingquLink.visible = false;
            }
            else
            {
                lingquLink.visible = true;
                if (this.user.guanzhi >= 27)
                {
                    lvupBtn.visible = false;
                }
                else
                {
                    lvupBtn.visible = true;
                }
            }
            this.onView(param1);
            this.gzlv = this.user.guanzhi;
            this.viewShow();
            guanname.url = "cs2_guanwei/gz" + this.gzlv + ".png";
            if ((this.gzlv + 1) < 28)
            {
                guanname1.url = "cs2_guanwei/gz" + (this.gzlv + 1) + ".png";
            }
            this.initRigthview();
            this.updateFenluFilter();
            return;
        }// end function

        private function updateFenluFilter() : void
        {
            this._fenluFilterShow = this.lingquLink.visible;
            if (this.visible && this._fenluFilterShow)
            {
                LightFactory.showlight(lingquLink);
            }
            else
            {
                LightFactory.clearlight(lingquLink);
            }
            return;
        }// end function

        public function lingqu() : void
        {
            this.roledata = UserData.getOnlineData();
            if (this.roledata.guanzhilingqu == 1)
            {
                lingquLink.visible = false;
                ModelProxy.change("mainview", "updateGuanZhi", null);
            }
            else
            {
                lingquLink.visible = true;
            }
            this.updateFenluFilter();
            return;
        }// end function

        public function getCount(param1:Object) : void
        {
            this.init();
            return;
        }// end function

        private function viewShow() : void
        {
            var _loc_1:int = 0;
            if (this.gzlv > 0)
            {
                att.text = this.cfggw[this.gzlv].phyatk + " - " + this.cfggw[this.gzlv].phyatkmax;
                msatt.text = this.cfggw[this.gzlv].magatk + " - " + this.cfggw[this.gzlv].magatkmax;
                dsatt.text = this.cfggw[this.gzlv].daoatk + " - " + this.cfggw[this.gzlv].daoatkmax;
                wfdef.text = this.cfggw[this.gzlv].phydef + " - " + this.cfggw[this.gzlv].phydefmax;
                msdef.text = this.cfggw[this.gzlv].magdef + " - " + this.cfggw[this.gzlv].magdefmax;
                money.text = this.cfggw[this.gzlv].money + "金币";
                box1.visible = true;
                if (this.cfggw[this.gzlv].guanyingname == "")
                {
                    dh_txt1.visible = false;
                }
                else
                {
                    dh_txt1.visible = true;
                }
                guanname.visible = true;
                wu_txt.visible = false;
                yupeiLink.label = this.cfggw[this.gzlv].guanyingname;
            }
            else
            {
                att.text = "0";
                msatt.text = "0";
                dsatt.text = "0";
                wfdef.text = "0";
                msdef.text = "0";
                guanname.visible = false;
                wu_txt.visible = true;
                box1.visible = false;
            }
            if ((this.gzlv + 1) <= 27)
            {
                view2.visible = true;
                _loc_1 = this.gzlv + 1;
                att1.text = this.cfggw[_loc_1].phyatk + " - " + this.cfggw[_loc_1].phyatkmax;
                msatt1.text = this.cfggw[_loc_1].magatk + " - " + this.cfggw[_loc_1].magatkmax;
                dsatt1.text = this.cfggw[_loc_1].daoatk + " - " + this.cfggw[_loc_1].daoatkmax;
                wfdef1.text = this.cfggw[_loc_1].phydef + " - " + this.cfggw[_loc_1].phydefmax;
                msdef1.text = this.cfggw[_loc_1].magdef + " - " + this.cfggw[_loc_1].magdefmax;
                needswtxt.text = this.cfggw[_loc_1].count;
                if (this.shengwang >= this.cfggw[(this.gzlv + 1)].count)
                {
                    sw_txt.color = 5485622;
                    LightFactory.showlight(lvupBtn);
                }
                else
                {
                    LightFactory.clearlight(lvupBtn);
                    sw_txt.color = 16711680;
                }
                money1.text = this.cfggw[_loc_1].money + "金币";
                if (this.cfggw[_loc_1].guanyingname == "")
                {
                    dh_txt2.visible = false;
                }
                else
                {
                    dh_txt2.visible = true;
                }
                yupeiLink1.text = this.cfggw[_loc_1].guanyingname;
            }
            else
            {
                view2.visible = false;
                needswtxt.text = "";
            }
            sw_txt.text = "" + this.shengwang;
            return;
        }// end function

        private function initRigthview() : void
        {
            var _loc_1:int = 0;
            this.showLevel(this.roleLv, lv, lv1, lv3);
            this.showLevel((this.roleLv - 1), lv4, lv5, lv6);
            if (this.roleLv < 100)
            {
                btn_duihuan.disabled = true;
                txt_times.x = 44;
                txt_times.text = "<font color=\'#94928c\'>人物等级</font><font color=\'#ff0000\'>100</font><font color=\'#94928c\'>级以上才可以兑换</font>";
                txt_exp.text = 0 + "";
            }
            else
            {
                _loc_1 = 3 - this.roledata.shengwanggetnum;
                txt_times.x = 77;
                if (_loc_1 > 0)
                {
                    txt_times.text = "今日剩余兑换次数：<font color=\'#31cf08\'>" + _loc_1 + "</font>";
                    btn_duihuan.disabled = false;
                }
                else
                {
                    btn_duihuan.disabled = true;
                    txt_times.text = "今日剩余兑换次数：<font color=\'#ff0000\'>" + _loc_1 + "</font>";
                }
                if (this.cfglevel[(this.roleLv - 1)])
                {
                    txt_exp.text = int(int(this.cfglevel[(this.roleLv - 1)]) / 1000) + "";
                }
            }
            return;
        }// end function

        private function renderHandler(param1:Component, param2:int) : void
        {
            var _loc_3:Object = null;
            var _loc_4:Object = null;
            var _loc_5:FlashItem = null;
            var _loc_6:Label = null;
            var _loc_7:Label = null;
            var _loc_8:Button = null;
            if (param2 < this.storeID.length)
            {
                _loc_3 = this.cfgstore[this.storeID[param2]];
                _loc_4 = ItemObject.create(this.cfgitem[_loc_3.itemid]);
                _loc_5 = param1.getChildByName("fitem") as FlashItem;
                _loc_5.data = _loc_4;
                _loc_6 = param1.getChildByName("txt_name") as Label;
                _loc_6.text = _loc_4.name;
                _loc_7 = param1.getChildByName("labelmoney") as Label;
                _loc_7.text = _loc_3.nowprice;
                _loc_8 = param1.getChildByName("btn_shiyong") as Button;
                _loc_8.clickHandler = new Handler(this.gobuy, [_loc_3]);
            }
            return;
        }// end function

        private function showLevel(param1:int, param2:Image, param3:Image, param4:Image) : void
        {
            if (param1 < 100)
            {
                param4.visible = false;
                param2.url = "png.common." + int(param1 / 10);
                param3.url = "png.common." + param1 % 10;
            }
            else
            {
                param4.visible = true;
                param2.url = "png.common." + String(param1).charAt(0);
                param3.url = "png.common." + String(param1).charAt(1);
                param4.url = "png.common." + String(param1).charAt(2);
            }
            return;
        }// end function

        private function gobuy(param1:Object) : void
        {
            var _loc_2:* = UserData.getBagData("money");
            if (_loc_2 < param1.nowprice)
            {
                ModelProxy.change("movetips", "error", "元宝不足，无法购买该道具！");
                return;
            }
            DialogMgr.instance.open(AppDialog.SC_BUY, {storeId:param1.id});
            return;
        }// end function

        private function addEvent() : void
        {
            closeBtn.addEventListener(MouseEvent.CLICK, this.onClose);
            guanzhiTab.selectHandler = new Handler(this.onView);
            lvupBtn.addEventListener(MouseEvent.CLICK, this.shengjiGW);
            duihuanLink.addEventListener(MouseEvent.CLICK, this.openRigth);
            close1btn.addEventListener(MouseEvent.CLICK, this.openRigth);
            btn_duihuan.addEventListener(MouseEvent.CLICK, this.duihuan);
            lingquLink.addEventListener(MouseEvent.CLICK, this.lianQu);
            yupeiLink.addEventListener(MouseEvent.CLICK, this.openYupei);
            txt_sw.addEventListener(MouseEvent.ROLL_OVER, this.onOver);
            txt_sw.addEventListener(MouseEvent.ROLL_OUT, this.outTex);
            txt_ry.addEventListener(MouseEvent.ROLL_OVER, this.onOver);
            txt_ry.addEventListener(MouseEvent.ROLL_OUT, this.outTex);
            return;
        }// end function

        protected function onOver(event:MouseEvent) : void
        {
            var _loc_2:String = "";
            switch(event.target.name)
            {
                case "1":
                {
                    _loc_2 = "声望获得途径：\n1.降妖伏魔任务\n2.声望兑换\n3.商城购买\n4.击杀BOSS获得";
                    break;
                }
                case "2":
                {
                    _loc_2 = "荣誉获得途径：\n1.定时活动击杀玩家获得荣誉值\n2.商城购买荣誉宝箱";
                    break;
                }
                default:
                {
                    break;
                }
            }
            ModelProxy.change("tips", "normal", {data:_loc_2, p:new Point()});
            return;
        }// end function

        protected function outTex(event:MouseEvent) : void
        {
            ModelProxy.change("tips", "state", "close");
            return;
        }// end function

        protected function openYupei(event:MouseEvent) : void
        {
            this.onView(1);
            return;
        }// end function

        public function getshengwan() : void
        {
            this.shengwang = UserData.getBagData("shengwang");
            txt_sw.name = "1";
            txt_ry.visible = false;
            txt_sw.visible = true;
            swtxt.text = "";
            return;
        }// end function

        public function getHonor(param1:Object = null) : void
        {
            var _loc_2:* = UserData.getBagData("honor");
            txt_ry.name = "2";
            txt_sw.visible = false;
            txt_ry.visible = true;
            swtxt.text = _loc_2 + "";
            if (param1 != null)
            {
                this.yupeiCtrl.openSelect(this.gzlv);
            }
            return;
        }// end function

        protected function lianQu(event:MouseEvent) : void
        {
            if (this.user.guanzhi > 0)
            {
                if (this.roledata.guanzhilingqu == 1)
                {
                    ModelProxy.change("movetips", "error", "你今日已经领取过了");
                }
                else
                {
                    PluginManager.instance.sendToBufferServer("guanwei", CMDGuanwei.Everyday, new Output());
                    WarningManager.show(61, [this.cfggw[this.gzlv].money]);
                }
            }
            return;
        }// end function

        protected function duihuan(event:MouseEvent) : void
        {
            PluginManager.instance.sendToBufferServer("guanwei", CMDGuanwei.DuiHSW, new Output());
            return;
        }// end function

        protected function openRigth(event:MouseEvent) : void
        {
            rigth_box.visible = !rigth_box.visible;
            if (rigth_box.visible)
            {
                this.initRigthview();
            }
            return;
        }// end function

        protected function shengjiGW(event:MouseEvent) : void
        {
            var _loc_3:Object = null;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:Object = null;
            if (this.gzlv >= 27)
            {
                return;
            }
            if (this.shengwang >= this.cfggw[(this.gzlv + 1)].count)
            {
                if (this.roleLv >= this.cfggw[(this.gzlv + 1)].level)
                {
                    PluginManager.instance.sendToServer("guanwei", CMDGuanwei.LevelUp, null);
                }
                else
                {
                    WarningManager.show(58);
                }
            }
            else
            {
                WarningManager.show(57);
            }
            var _loc_2:* = ModelProxy.getValue("data", "jingjitypedata");
            if (_loc_2 == null)
            {
                return;
            }
            if (_loc_2[8])
            {
                _loc_3 = ConfigDictionary.data.activitygoal;
                _loc_4 = UserData.getUserData("guanzhi");
                for each (_loc_6 in _loc_3[8])
                {
                    
                    _loc_5 = _loc_5 + 1;
                    if (_loc_2[8][0][(_loc_5 - 1)].isGet == 0 && _loc_4 >= _loc_6.goal && _loc_2[8][0][(_loc_5 - 1)].count < _loc_6.count)
                    {
                        ModelProxy.change("mainui", "showEffect", {ab:"kaifujingji", state:true});
                        break;
                    }
                }
            }
            return;
        }// end function

        public function onView(param1:int) : void
        {
            if (param1 < 0)
            {
                return;
            }
            guanzhiTab.selectedIndex = param1;
            switch(param1)
            {
                case 0:
                {
                    this.getshengwan();
                    view_gw.visible = true;
                    this.yupeiCtrl.visible = false;
                    this.yupeiCtrl.clearTime();
                    this.yupeiCtrl.clearTime2();
                    break;
                }
                case 1:
                {
                    this.yupeiCtrl.openSelect(this.gzlv);
                    this.getHonor();
                    this.yupeiCtrl.visible = true;
                    this.yupeiCtrl.setMove();
                    rigth_box.visible = false;
                    view_gw.visible = false;
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        protected function onClose(event:MouseEvent) : void
        {
            ModelProxy.change("plugin", "close", "guanwei");
            return;
        }// end function

        public function closeComplete() : void
        {
            if (this.yupeiCtrl)
            {
                this.yupeiCtrl.closeComplete();
            }
            this.updateFenluFilter();
            if (this.guanweiMc)
            {
                if (this.guanweiMc.parent)
                {
                    this.guanweiMc.dispose();
                    this.guanweiMc.parent.removeChild(this.guanweiMc);
                    this.guanweiMc = null;
                }
            }
            LightFactory.clearlight(lingquLink);
            LightFactory.clearlight(lvupBtn);
            return;
        }// end function

    }
}
